var QQMapWX = require('../../libs/qqmap-wx-jssdk.js'); //腾讯地图sdk
var qqmapsdk;
// let plugin = requirePlugin("subway");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: "",
    longitude: "",
    status: "",
    type: {
      "driving": "驾车",
      "walking": "步行"
    },
    marker: [{ //marker标记
      id: 0,
      latitude: null,
      longitude: null,
      iconPath: "/images/Marker.png",
      width: 40,
      height: 40,
      callout: {
        content: "",
        fontSize: 14,
        color: '#000000',
        bgColor: '#ffffff',
        padding: 8,
        borderRadius: 4,
        boxShadow: '4px 8px 16px 0 rgba(0)'
      },
    }]
  },
  marker(e) {
    console.log("你点击了marker", e)
  },
  callout(e) {
    console.log("你点击了callout", e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    qqmapsdk = new QQMapWX({ //腾讯api核内实例化
      key: '234BZ-UVRK3-47M3O-YINT2-DVECJ-VJFY7'
    });
    this.getLocation()
  },
  type(e) {
    var that = this
    console.log("出行方式", e)
    that.setData({
      status: e.currentTarget.dataset.status
    })
    qqmapsdk.calculateDistance({
      mode: e.currentTarget.dataset.status,
      to: [{
        latitude: that.data.marker[0].latitude,
        longitude: that.data.marker[0].longitude
      }],
      success(res) {
        console.log(that.data.status + "行驶距离", res)
        that.setData({
          distance: Math.floor(res.result.elements[0].distance / 100) / 10,
          time: Math.ceil(res.result.elements[0].duration / 60)
        })
      }
    })
    
  },
  backfill: function(e) { //数据回填方法
    console.log("数据回填", e)
    var that = this
    var id = e.currentTarget.id;
    for (var i = 0; i < that.data.suggestion.length; i++) {
      if (i == id) {
        that.setData({
          backfill: that.data.suggestion[i].title,
          latitude: that.data.suggestion[i].latitude,
          longitude: that.data.suggestion[i].longitude,
          'marker[0].latitude': that.data.suggestion[i].latitude,
          'marker[0].longitude': that.data.suggestion[i].longitude,
          'marker[0].callout.content': that.data.suggestion[i].title
        });
        let plugin = requirePlugin('routePlan');
        let key = '234BZ-UVRK3-47M3O-YINT2-DVECJ-VJFY7';  //使用在腾讯位置服务申请的key
        let referer = '地图功能';   //调用插件的app的名称
        let endPoint = JSON.stringify({  //终点
          'name': that.data.backfill,
          'latitude': that.data.marker[0].latitude,
          'longitude': that.data.marker[0].longitude
        });
        wx.navigateTo({
          url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
        });
        // if (that.data.status) {//数据回填判断行走路线
        //   console.log("前", that.data.status)
        //   qqmapsdk.calculateDistance({
        //     mode: that.data.status,
        //     to: [{
        //       latitude: that.data.marker[0].latitude,
        //       longitude: that.data.marker[0].longitude
        //     }],
        //     success(res) {
        //       console.log(that.data.status + "行驶距离", res)
        //       let plugin = requirePlugin('routePlan');
        //       let key = '234BZ-UVRK3-47M3O-YINT2-DVECJ-VJFY7';  //使用在腾讯位置服务申请的key
        //       let referer = '地图功能';   //调用插件的app的名称
        //       let endPoint = JSON.stringify({  //终点
        //         'name': that.data.backfill,
        //         'latitude': that.data.marker[0].latitude,
        //         'longitude': that.data.marker[0].longitude
        //       });
        //       wx.navigateTo({
        //         url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
        //       });
        //       that.setData({
        //         distance: Math.floor(res.result.elements[0].distance / 100) / 10,
        //         time: Math.ceil(res.result.elements[0].duration / 60)
        //       })
        //     }
        //   })
            
        // }
      }
    }
  },
  getPlace(e) { //地点关键词搜索
    // console.log("地点关键词搜索", e)
    var that = this
    var sug = [];
    qqmapsdk.getSuggestion({
      keyword: e.detail.value,
      page_size: 20,
      success(res) {
        console.log("地点搜索", res)
        for (var i = 0; i < res.data.length; i++) {
          sug.push({ // 获取返回结果，放到sug数组中
            title: res.data[i].title,
            id: res.data[i].id,
            addr: res.data[i].address,
            city: res.data[i].city,
            district: res.data[i].district,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng
          });
        }
        that.setData({ //设置suggestion属性，将关键词搜索结果以列表形式展示
          suggestion: sug
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.getLocation()
    // this.getCity()
  },
  getLocation: function() { //腾讯地图逆地址解析
    var that = this
    qqmapsdk.reverseGeocoder({
      get_poi: 1,
      success(res) {
        console.log("逆地址解析", res)
        var latitude = res.result.location.lat
        var longitude = res.result.location.lng
        that.setData({
          latitude: latitude,
          longitude: longitude,
          'marker[0].latitude': latitude,
          'marker[0].longitude': longitude,
          'marker[0].callout.content': res.result.pois[0].title
        })
        // console.log(that.data.marker[0].latitude)
        // console.log(that.data.marker[0].longitude)
      },
      fail(err) {
        console.log("逆地址解析错误信息", err)
      }
    })
  },
  getCity() { //获取城市列表
    var _this = this;
    //调用获取城市列表接口
    qqmapsdk.getCityList({
      success: function(res) { //成功后的回调
        console.log(res);
        console.log('省份数据：', res.result[0]); //打印省份数据
        console.log('城市数据：', res.result[1]); //打印城市数据
        console.log('区县数据：', res.result[2]); //打印区县数据
      },
      fail: function(error) {
        console.error(error);
      },
      complete: function(res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})